var total=0;
var UILoading;
apiready = function(){
if(api.connectionType=='none'){
	error();
  var reloading = $api.byId('reloading');
  $api.addEvt(reloading, 'click', function(){
  	_openFrame(1)
  });
 
  	return;
  }

api.setFrameAttr({
            name: api.frameName,
            customRefreshHeader:"UIPullRefreshFlash",
            bounces:true
        });
/*UILoading = api.require('UILoading');
UILoading.keyFrame({
    rect: {
        w: 80,                              
        h: 100      
    },
    styles: {
        bg: 'rgba(0,0,0,0.5)', 
        corner: 5, 
        interval: 50,
        frame:{ 
            w: 80, 
            h: 80
        }
    }
}, function(ret){   
    alert(JSON.stringify(ret));
});*/
	getLotteryData(true)
     api.setCustomRefreshHeaderInfo({
            bgColor : '#f0f0f0',
            image: {
        pull: 'widget://image/refresh/1.png', 
        transform: [
            'widget://image/refresh/1.png', 
            'widget://image/refresh/2.png',
            'widget://image/refresh/3.png',
            'widget://image/refresh/4.png',
            'widget://image/refresh/5.png',
            'widget://image/refresh/6.png',
            'widget://image/refresh/7.png'
        ],
        load: [ 
            'widget://image/refresh/1.png', 
            'widget://image/refresh/2.png',
            'widget://image/refresh/3.png', 
            'widget://image/refresh/4.png',
            'widget://image/refresh/5.png'
        ]
    }
        }, function() {
        
            if(api.connectionType!='none'){
  	var typeName = $api.getStorage('type');    
	        //alert(typeName)	      
            //下拉刷新被触发，自动进入加载状态，使用 api.refreshHeaderLoadDone() 手动结束加载中状态      
            setTimeout(function(){             
            getLotteryData(true)
	        showProgress()
	        api.refreshHeaderLoadDone();
	    }, 800);
  }else{
  	 setTimeout(function(){          
  	 		toast("没有网络，请检查网络设置")          
	        api.refreshHeaderLoadDone();
	    }, 800);
  }
           
        });
    
    //上拉加载
    api.addEventListener({

        name: 'scrolltobottom',
        extra: {
            threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
        }
    }, function (ret, err) {

        $("#more").show();
        var _page=parseInt($api.getStorage("currentPage_2"));
       
        if(_page+10<=500){
        	//alert(_page);
         getLotteryData(false,_page+10);
        $api.setStorage("currentPage_2", _page+10); // 页码+1
        }else{
            $("#more").hide();
        }
      
       
    });
    
    }

    //获取揭晓记录
function getLotteryData(isReload,currentPage_2){
	var pageSize = 10;
	currentPage_2=isReload?0:currentPage_2;
        api.ajax({
        url: 'http://wap.jqmao.cn/getLotteryData',
        method: 'post',
        data: {
            values: {pageSize: pageSize,currentPage_2:currentPage_2}
        },
        dataType: 'json'
    }, function (ret, err) {

        if (ret) {
        
        	total = ret['count']
        	
        	//total = ret['total']
            var content = $api.byId('lottery-content');
            //$api.remove('content');
            var tpl = $api.byId('lottery-template').text;
            var tempFn = doT.template(tpl);
           
            //UILoading.closeKeyFrame();
            if (isReload) {
            	api.hideProgress();
                $api.setStorage("currentPage_2", 0);        // 重新设置为0
                $api.html(content, tempFn(ret['listItems']));
                var itemList = ret['listItems'];
                var l = parseInt(itemList.length)-1
                for(var i=l; i>=0; i--){
               
                	if(itemList[i].remark==0){
                	var html = '<dt onclick="openGoodsDetail('+itemList[i].id+')">'+
                	'<div class="lotteryItem"><div class="lottery-item-left"><div class="good-Img">'+
                	'<img src="http://wap.jqmao.cn/statics/uploads/'+itemList[i].thumb+'" width="80px" height="80px" alt="" /></div></div>'+
                	'<div class="lottery-item-right"><div class="good-about">'+
                	'<p class="lottery-title">(第'+itemList[i].qishu+'期)'+itemList[i].title+'</p><p class="gray">价值：<span>￥'+itemList[i].money+'</span>'+
                	'</p></div><div class="winningInfo"><div style="line-height: 28px;">'+
                	'<div style="display:none" class="winningInfo"><div class="winning-info-left"><p class="username gray">中奖者：<span class="blue">'+itemList[i].q_user+'</span></p>'+
					'<p class="buyNum gray">本期猫购：<span class="red">'+itemList[i].gonumber+'</span>人次<p class="gray">幸运猫购码：<span class="blue">'+itemList[i].q_user_code+'</span></p>'+
					'<p class="q-end-time gray">揭晓时间：<span >'+itemList[i].q_end_time+'</span></p></div></div>'+
                	'<p id="djs" class="gray">揭晓倒计时：</p><div id="'+itemList[i].id+'" style="font-size: 20px;color:#FF0000">'+
                	'<em class="mini">00</em>分<em class="sec">00</em>秒<em class="hm">0</em><em class="hm">0</em></div>'+
                	'</div></div></div></div></dt>';
                	var firstel = $api.first(content,'dt');
                		$api.before(firstel,html);
                		
                		$('#'+itemList[i].id).fnTimeCountDown(itemList[i].Remaining_second);
                		
                		
                	}
                }
                
            } else {
                $api.append(content, tempFn(ret['listItems']));
            }
        } else {
            ////alert(JSON.stringify(err))
        }
    });
    
}




    

    